package com.gewu.pm.ui.adapter.sales;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.SalesLeadBean;

/**
 * 销售线索适配器
 */
public class SalesLeadAdapter extends AppAdapter<SalesLeadBean> {

    // 类型常量
    private static final int TYPE_UNCONVERTED = 0;  // 未转化
    private static final int TYPE_CONVERTED = 1;    // 已转化

    private OnConvertClickListener mConvertClickListener;  // 转终端点击监听

    public SalesLeadAdapter(Context context) {
        super(context);
    }

    /**
     * 设置转终端点击监听
     */
    public void setOnConvertClickListener(OnConvertClickListener listener) {
        this.mConvertClickListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        SalesLeadBean item = getItem(position);
        return item.getType();  // 根据 Bean 的 type 返回类型
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_CONVERTED) {
            return new ConvertedViewHolder();
        } else {
            return new UnconvertedViewHolder();
        }
    }

    /**
     * 未转化 ViewHolder
     */
    private final class UnconvertedViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mLeadNameView;
        private final TextView mLeadAddressView;
        private final TextView mLeadCodeView;
        private final TextView mConvertButton;

        private UnconvertedViewHolder() {
            super(R.layout.sales_lead_unconverted_item);
            mLeadNameView = findViewById(R.id.tv_lead_name);
            mLeadAddressView = findViewById(R.id.tv_lead_address);
            mLeadCodeView = findViewById(R.id.tv_lead_code);
            mConvertButton = findViewById(R.id.btn_convert);

            // 转终端按钮点击事件
            mConvertButton.setOnClickListener(new View.OnClickListener() {
                @SingleClick
                @Override
                public void onClick(View v) {
                    int position = getViewHolderPosition();
                    if (mConvertClickListener != null) {
                        mConvertClickListener.onConvertClick(position, getItem(position));
                    }
                }
            });
        }

        @Override
        public void onBindView(int position) {
            SalesLeadBean item = getItem(position);
            mLeadNameView.setText(item.getName());
            // 其他数据已在布局中写死
        }
    }

    /**
     * 已转化 ViewHolder
     */
    private final class ConvertedViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mTerminalNameView;
        private final TextView mTerminalAddressView;
        private final TextView mTerminalCodeView;
        private final TextView mConverterView;
        private final TextView mConversionTimeView;

        private ConvertedViewHolder() {
            super(R.layout.sales_lead_converted_item);
            mTerminalNameView = findViewById(R.id.tv_terminal_name);
            mTerminalAddressView = findViewById(R.id.tv_terminal_address);
            mTerminalCodeView = findViewById(R.id.tv_terminal_code);
            mConverterView = findViewById(R.id.tv_converter);
            mConversionTimeView = findViewById(R.id.tv_conversion_time);
        }

        @Override
        public void onBindView(int position) {
            SalesLeadBean item = getItem(position);
            mTerminalNameView.setText(item.getName());
            // 其他数据已在布局中写死
        }
    }

    /**
     * 转终端点击监听接口
     */
    public interface OnConvertClickListener {
        void onConvertClick(int position, SalesLeadBean leadBean);
    }
}
